package de.tum.wi.som.rcpsp.view;

import java.awt.Color;
import java.awt.Dimension;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

import de.tum.wi.som.rcpsp.entities.Activity;
import de.tum.wi.som.rcpsp.entities.ResourceAssignment;
import de.tum.wi.som.rcpsp.entities.Schedule;
import de.tum.wi.som.rcpsp.entities.TimePeriod;

public class RCPSPBarChart extends ApplicationFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Schedule schedule;
	private List<Activity> activities;

	public RCPSPBarChart(String s, Schedule schedule, List<Activity> actitvities) {
		super(s);
		this.schedule = schedule;
		this.activities = actitvities;
		JPanel jpanel = createDemoPanel();
		jpanel.setPreferredSize(new Dimension(500, 270));
		setContentPane(jpanel);
	}

	private CategoryDataset createDataset() {
		List<TimePeriod> timePeriod = schedule.getTimePeriods();
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for (TimePeriod timeUnit : timePeriod) {
			List<ResourceAssignment> resourceAssignments = timeUnit
					.getResourceAssignments();
			int time = timeUnit.getTime() + 1;
			for (ResourceAssignment resAssignment : resourceAssignments) {
				dataset.addValue(resAssignment.getResourceUnits(),
						getIdForActivity(resAssignment.getActivity()), time
								+ "");
			}
		}

		return dataset;
	}

	private String getIdForActivity(Activity activity) {
		return activity.getName()  + " (" + activity.getUpperBound() + "/"
				+ activity.getLowerBound() + ")";
	}

	private JFreeChart createChart(CategoryDataset categorydataset) {
		int t = calculateT();
		int lb = schedule.getLowerBoundProjectEnd();
		double deltaLB = ((double)Math.round(((1 - (double) lb / (double) t)*10000))/100.0);
		JFreeChart jfreechart = ChartFactory.createStackedBarChart("R: "
				+ schedule.getWorkContentResource().getLimit() + ", m: "
				+ schedule.getWorkContentResource().getMinBlockLength()
				+ ", #Akt.:" + calculateNumberOfActivities() + ", Opt. T: "
				+ lb + ", T:" + t + ", %Delta:" + deltaLB, "Zeitperiode",
				"Ressourcenzuordnung", categorydataset,
				PlotOrientation.VERTICAL, true, true, false);
		jfreechart.setBackgroundPaint(Color.white);
		CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();
		categoryplot.setBackgroundPaint(Color.lightGray);
		categoryplot.setRangeGridlinePaint(Color.white);
		NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
		numberaxis.setTickUnit(new NumberTickUnit(1.0));
		CategoryAxis domainAxis = categoryplot.getDomainAxis();
		domainAxis.setCategoryMargin(0.1);
		domainAxis.setLowerMargin(0);
		domainAxis.setUpperMargin(0);
		StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot
				.getRenderer();
		stackedbarrenderer.setDrawBarOutline(false);
		stackedbarrenderer.setBaseItemLabelsVisible(true);
		stackedbarrenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		return jfreechart;
	}

	private int calculateT() {
		int t = 0;
		for (TimePeriod time : schedule.getTimePeriods()) {
			if (time.getAvailableResourcesPerTime() != schedule
					.getWorkContentResource().getLimit() && time.getTime() > t) {
				t = time.getTime();
			}
		}
		t++;
		return t;
	}

	private int calculateNumberOfActivities() {
		int i = 0;
		for (Activity act : activities) {
			if (act.isPlanned()) {
				i++;
			}
		}
		return i;
	}

	

	public JPanel createDemoPanel() {
		JFreeChart jfreechart = createChart(createDataset());
		return new ChartPanel(jfreechart);
	}
}
